//
//  AddFriendVC.m
//  BrushFace
//
//  Created by timer_q on 16/1/9.
//  Copyright © 2016年 Zzz. All rights reserved.
//

#import "AddFriendVC.h"
#import "AddFriendHeaderView.h"
#import "ContactViewModel.h"
#import "AddFriendCell.h"
#import "NavHelper.h"
#import <MessageUI/MessageUI.h>
#import "SearchResultVC.h"
@interface AddFriendVC ()<UITableViewDataSource,UITableViewDelegate,MFMessageComposeViewControllerDelegate>
{
    UITableView *_tableView;
    ContactViewModel *_reqModel;
    NSMutableArray *_dataArray;
}
@end

@implementation AddFriendVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self UIConfig];
    _reqModel = [ContactViewModel SceneModel];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self dataInit];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - customMethods
- (void)dataInit{
    
    _dataArray = [[NSMutableArray alloc] init];
    [_reqModel addressBook_GetNewFriendListAndCB:^(id output) {
        [_dataArray removeAllObjects];
        [_dataArray addObjectsFromArray:[output objectForKey:@"friends"]];
        [_tableView reloadData];
    }];

}
- (void)UIConfig{
    [self.navBar setTitle:@"添加朋友"];
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_BLUE];
    self.view.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, SCREEN_WIDTH, SCREEN_HEIGHT-65) style:UITableViewStylePlain];
    _tableView.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableHeaderView = [self tableViewHeaderView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];

}
- (UIView *)tableViewHeaderView{
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 140)];
    backView.backgroundColor = [UIColor clearColor];
    AddFriendHeaderView *headerView = [AddFriendHeaderView loadBundleNib];
    headerView.frame = CGRectMake(0, 10, SCREEN_WIDTH, 130);
    [backView addSubview:headerView];
    return backView;
}

#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AddFriendCell *cell = [AddFriendCell loadBundleNib];
    NSDictionary *dict = [_dataArray objectAtIndex:indexPath.row];
    cell.nickNameLabel.text = [dict objectForKey:@"friendnickname"];
    cell.phoneNameLabel.text = [NSString stringWithFormat:@"手机联系人：%@",[dict objectForKey:@"friendname"]];
    [cell.headerImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HEADERIMAGE_FETCH,[dict objectForKey:@"frienduserid"]]]];
    NSInteger statu = [[NSString stringWithFormat:@"%@",[dict objectForKey:@"status"]] integerValue];
    cell.phone = [NSString stringWithFormat:@"%@",dict[@"friendphone"]];
    cell.statu = statu;
    cell.target = self;
    cell.friendId = dict[@"frienduserid"];
    cell.msgid = dict[@"id"];
    switch (statu) {
        case 0:
        {
            [cell.statuBtn setImage:[UIImage imageNamed:@"yaoqingBtn"] forState:UIControlStateNormal];
            cell.statulabel.hidden = YES;
            cell.statuBtn.hidden = NO;
        }
            break;
        case 1:
        {
            cell.statulabel.hidden = YES;
            cell.statuBtn.hidden = NO;
            [cell.statuBtn setImage:[UIImage imageNamed:@"addfrienddetail"] forState:UIControlStateNormal];
        }
            break;
        case 2:
        {
            cell.statulabel.text = @"待通过";
            cell.statulabel.hidden = NO;
            cell.statuBtn.hidden = YES;
        }
            break;
        case 3:
        {
            cell.statuBtn.hidden = NO;
            cell.statulabel.hidden = YES;
            [cell.statuBtn setImage:[UIImage imageNamed:@"jieshou"] forState:UIControlStateNormal];
        }
            break;
        case 4:
        {
            cell.statulabel.text = @"已添加";
            cell.statulabel.hidden = NO;
            cell.statuBtn.hidden = YES;
        }
            break;
        default:
            break;
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
#pragma mark-------qiweipeng

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
//    view.backgroundColor = [UIColor clearColor];
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
//    label.text = @"新的朋友";
//    label.textColor = [UIColor lightGrayColor];
//    label.font = [UIFont systemFontOfSize:14];
//    [view addSubview:label];
//    return view;
//}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"新的朋友";
}
#pragma mark------------------
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dict = [_dataArray objectAtIndex:indexPath.row];
    NSInteger statu = [[NSString stringWithFormat:@"%@",[dict objectForKey:@"status"]] integerValue];
    NSString *friendId = dict[@"frienduserid"];
    switch (statu) {
        case 0:
        {return;}break;
        case 1:
        {
            SearchResultVC *resultVC = [[SearchResultVC alloc] init];
            resultVC.source = @"通讯录匹配";
            resultVC.userid = friendId;
            [resultVC setHidesBottomBarWhenPushed:YES];
            [self.navigationController pushViewController:resultVC animated:YES];
        }
            break;
        case 2:
        {
            SearchResultVC *resultVC = [[SearchResultVC alloc] init];
            resultVC.source = @"通讯录匹配";
            resultVC.userid = friendId;
            [resultVC setHidesBottomBarWhenPushed:YES];
            [self.navigationController pushViewController:resultVC animated:YES];
        }
            break;
        case 3:
        {
            [NavHelper gotoPassFriendVCWithUserID:dict[@"id"]];
        }
            break;
        case 4:
        {
            [NavHelper gotoContactDetailVCWithUserId:friendId];
        }
            break;
        default:
            break;
    }
    

}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    switch (result)
    {
        case MessageComposeResultCancelled:
            //LOG_EXPR(@"Result: SMS sending canceled");
            break;
        case MessageComposeResultSent:
        {
            
        }
            break;
        case MessageComposeResultFailed:
        {
            
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"发送失败" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            [alert show];
        }
            break;
        default:
            //LOG_EXPR(@"Result: SMS not sent");
            break;
    }
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
@end
